﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMSSystem.DYB;
using WMSSystem.WYJ.Dto.Ledger;
using WMSSystem.WYJ.Interfance;
using WMSSystem.YJ;

namespace WMSSystem.WYJ
{
    public class InventServices : ApplicationService, IInventServices
    {
        private readonly IRepository<Commoditiestable, Guid> _commodRepository;
        private readonly IRepository<WarehouseManagement, Guid> _warehouseRepository;
        private readonly IRepository<LocationManagement, Guid> _locationRepository;
        private readonly IRepository<PositionManagement, Guid> _positionRepository;
        private readonly IRepository<InventoryDetails, Guid> _inventoryDetailsRepository;
        private readonly IRepository<Inboundmanagement, Guid> _inboundmanagementRepository;
        private readonly IRepository<OutbountDetails, Guid> _outbounditiesRepository;
        private readonly IRepository<Outbound, Guid> _outboundRepository;
        private readonly IRepository<InventoryGoods, Guid> _inventGoodsRepository;

        public InventServices(IRepository<Commoditiestable, Guid> commodRepository, IRepository<WarehouseManagement, Guid> warehouseRepository, IRepository<LocationManagement, Guid> locationRepository, IRepository<PositionManagement, Guid> positionRepository, IRepository<InventoryDetails, Guid> inventoryDetailsRepository, IRepository<Inboundmanagement, Guid> inboundmanagementRepository, IRepository<OutbountDetails, Guid> outbounditiesRepository, IRepository<Outbound, Guid> outboundRepository, IRepository<InventoryGoods, Guid> inventGoodsRepository)
        {
            _commodRepository = commodRepository;
            _warehouseRepository = warehouseRepository;
            _locationRepository = locationRepository;
            _positionRepository = positionRepository;
            _inventoryDetailsRepository = inventoryDetailsRepository;
            _inboundmanagementRepository = inboundmanagementRepository;
            _outbounditiesRepository = outbounditiesRepository;
            _outboundRepository = outboundRepository;
            _inventGoodsRepository = inventGoodsRepository;
        }



        /// <summary>
        /// 库存查询
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<InventoryInquiry>> GetInventoryInquiryListAsync()
        {
            var commond=await _commodRepository.GetListAsync();
            var location=await _locationRepository.GetListAsync();
            var inventoryDetails=await _inventoryDetailsRepository.GetListAsync();
            var outboundes = await _outbounditiesRepository.GetListAsync();
            var inventgoods=await _inventGoodsRepository.GetListAsync();
            var inbound=await _inboundmanagementRepository.GetListAsync();
            var positions=await _positionRepository.GetListAsync();

            var query = from a in commond
                        join b in inventoryDetails on a.Id equals b.CommoditiesId
                        join c in inbound on b.InboundmanagementCode equals c.Code
                        join d in location on b.Positionid equals d.Id
                        join e in inventgoods on a.Id equals e.CommoditiesId
                        join f in outboundes on a.Id equals f.CommoditiesId


                        select new InventoryInquiry
                        {
                            CommoditiesCode = a.CommoditiesCode,
                            CommoditiesName = a.CommoditiesName,
                            CommoditiesStates = a.CommoditiesStates,
                            CommoditiesType = a.CommoditiesType,
                            CommoditiesGuike = a.CommoditiesGuike,
                            CommoditiesUnit = a.CommoditiesUnit,
                            CommoditiesUnitcode = a.CommoditiesUnitcode,
                            Namewei = d.Name,
                            Inventory = e.Inventory,
                            Date=c.Date,
                            Num = c.Num,
                            Price = c.Price,
                            Numout = f.Num,


                        };
            return query.ToList();

        }
        /// <summary>
        /// 出入库流水
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<StockInandOutFlow>> GetStockInandAsync()
        {
            var inbound = await _inboundmanagementRepository.GetListAsync();
            var outbound = await _outboundRepository.GetListAsync();
            var warehouse = await _warehouseRepository.GetListAsync();
            var query = from a in inbound
                        join b in warehouse on a.Warehouseid equals b.Id
                        join c in outbound on b.Id equals c.Warehouseid
                        select new StockInandOutFlow
                        {
                            Code = a.Code,
                            Type = a.Type,
                            Date = a.Date,
                            Warehouseid = a.Warehouseid,
                            Manager = a.Manager,
                            Num = a.Num,
                            Price = a.Price,
                            Codeout = c.Code,
                            Typeout = c.Type,
                            Dateout = c.Date,
                            Warehouseidout = c.Warehouseid,
                            Numout = c.Num,
                            Priceout = c.Price,
                        };
            return query.ToList();
        }
    }
}
